package javamail;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;



public class Mail {
	private String Senduser = "x50269x@gmail.com";				//email address
	private String Senduserpwd = "john83425";			//email password
	private String HostName = "smtp.gmail.com";	//用gmail寄送的設定
	private String SSLSmtpPort = "465";			//用gmail寄送的設定
	
	
	public void sendMail(String subject,String[] recipient,String message){		
		try {
			HtmlEmail email = new HtmlEmail();
			email.setHostName(HostName);
			email.setSmtpPort(465);
			email.setAuthenticator(new DefaultAuthenticator(Senduser, Senduserpwd));
			email.setDebug(true);
			/****************設定****************/
			email.getMailSession().getProperties().put("mail.smtps.auth", "true");
			email.getMailSession().getProperties().put("mail.debug", "true");
			email.getMailSession().getProperties().put("mail.smtps.port", SSLSmtpPort);
			email.getMailSession().getProperties().put("mail.smtps.socketFactory.port", SSLSmtpPort);
			email.getMailSession().getProperties().put("mail.smtps.socketFactory.class",   "javax.net.ssl.SSLSocketFactory");
			email.getMailSession().getProperties().put("mail.smtps.socketFactory.fallback", "false");
			email.getMailSession().getProperties().put("mail.smtp.starttls.enable", "true");
			/****************結束設定****************/
			email.setCharset("UTF-8");				//編碼
			email.setSubject(subject);				//主旨
		    email.setFrom(Senduser, "測試寄件者");	//顯示寄件者的名稱
		    email.setHtmlMsg(message);				//內容
		 for (int i = 0; i < recipient.length; i++) { 
			 email.addTo(recipient[i], "收件者");	
			 System.out.println(recipient[i]);//收件者的email
			}
			email.send();
		    System.out.println("傳送成功!");
		   
		} catch (EmailException e) {
		    e.printStackTrace();
		}	
	}
	
}
